{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/pixeltable/pixeltable/blob/master/docs/release/tutorials/rag-demo.ipynb)&nbsp;&nbsp;\n",
    "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pixeltable/pixeltable/blob/master/docs/release/tutorials/rag-demo.ipynb)\n",
    "\n",
    "# Document Indexing and RAG"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial, we'll demonstrate how RAG operations can be implemented in Pixeltable. In particular, we'll develop a RAG application that summarizes a collection of PDF documents and uses ChatGPT to answer questions about them.\n",
    "\n",
    "In a traditional RAG workflow, such operations might be implemented as a Python script that runs on a periodic schedule or in response to certain events. In Pixeltable, they are implemented as persistent tables that are updated automatically and incrementally as new data becomes available.\n",
    "\n",
    "**If you are running this tutorial in Colab:**\n",
    "In order to make the tutorial run a bit snappier, let's switch to a GPU-equipped instance for this Colab session. To do that, click on the `Runtime -> Change runtime type` menu item at the top, then select the `GPU` radio button and click on `Save`.\n",
    "\n",
    "We first set up our OpenAI API key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "OpenAI API Key: ········\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import getpass\n",
    "if 'OPENAI_API_KEY' not in os.environ:\n",
    "    os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then install the packages we need for this tutorial and then set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q pixeltable sentence-transformers tiktoken openai openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connected to Pixeltable database at: postgresql://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata\n",
      "Created directory `rag_demo`.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pixeltable as pxt\n",
    "\n",
    "pxt.drop_dir('rag_demo', force=True)  # Ensure a clean slate for the demo\n",
    "pxt.create_dir('rag_demo')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we'll create a table containing the sample questions we want to answer. The questions are stored in an Excel spreadsheet, along with a set of \"ground truth\" answers to help evaluate our model pipeline. We can use Pixeltable's handy `import_excel()` utility to load them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Created table `queries`.\n",
      "Computing cells:   0%|                                                   | 0/16 [00:00<?, ? cells/s]\n",
      "Inserting rows into `queries`: 8 rows [00:00, 2892.12 rows/s]\n",
      "Computing cells: 100%|████████████████████████████████████████| 16/16 [00:00<00:00, 3342.41 cells/s]\n",
      "Inserted 8 rows with 0 errors.\n"
     ]
    }
   ],
   "source": [
    "# download the spreadsheet\n",
    "import urllib.request\n",
    "base = 'https://github.com/pixeltable/pixeltable/raw/master/docs/source/data/rag-demo/'\n",
    "qa_url = base + 'Q-A-Rag.xlsx'\n",
    "qa_filename, _ = urllib.request.urlretrieve(qa_url)\n",
    "\n",
    "# and create a table from it\n",
    "queries_t = pxt.io.import_excel('rag_demo.queries', qa_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>S__No_</th>\n",
       "      <th>Question</th>\n",
       "      <th>correct_answer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>What is roughly the current mortage rate?</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>What is the current dividend yield for Alphabet Inc. (\\$GOOGL)?</td>\n",
       "      <td>0.0046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>What is the market capitalization of Alphabet?</td>\n",
       "      <td>\\$2182.8 Billion</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>What are the latest financial metrics for Accenture PLC?</td>\n",
       "      <td>missed consensus forecasts and strong total bookings rising by 22% annually</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>What is the overall latest rating for Amazon.com from analysts?</td>\n",
       "      <td>SELL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>What is the operating cash flow of Amazon in Q1 2024?</td>\n",
       "      <td>18,989 Million</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>What is the expected EPS for Nvidia in Q1 2026?</td>\n",
       "      <td>0.73 EPS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>What are the main reasons to buy Nvidia?</td>\n",
       "      <td>Datacenter, GPUs Demands, Self-driving, and cash-flow</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "   S__No_                                           Question  \\\n",
       "0       1          What is roughly the current mortage rate?   \n",
       "1       2  What is the current dividend yield for Alphabe...   \n",
       "2       3     What is the market capitalization of Alphabet?   \n",
       "3       4  What are the latest financial metrics for Acce...   \n",
       "4       5  What is the overall latest rating for Amazon.c...   \n",
       "5       6  What is the operating cash flow of Amazon in Q...   \n",
       "6       7    What is the expected EPS for Nvidia in Q1 2026?   \n",
       "7       8           What are the main reasons to buy Nvidia?   \n",
       "\n",
       "                                      correct_answer  \n",
       "0                                               0.07  \n",
       "1                                             0.0046  \n",
       "2                                    $2182.8 Billion  \n",
       "3  missed consensus forecasts and strong total bo...  \n",
       "4                                               SELL  \n",
       "5                                     18,989 Million  \n",
       "6                                           0.73 EPS  \n",
       "7  Datacenter, GPUs Demands, Self-driving, and ca...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Outline\n",
    "\n",
    "There are two major parts to our RAG application:\n",
    "\n",
    "1. Document Indexing: Load the documents, split them into chunks, and index them using a vector embedding.\n",
    "2. Querying: For each question on our list, do a top-k lookup for the most relevant chunks, use them to construct a ChatGPT prompt, and send the enriched prompt to an LLM.\n",
    "\n",
    "We'll implement both parts in Pixeltable.\n",
    "\n",
    "## 1. Document Indexing\n",
    "\n",
    "All data in Pixeltable, including documents, resides in tables.\n",
    "\n",
    "Tables are persistent containers that can serve as the store of record for your data. Since we are starting from scratch, we will start with an empty table `rag_demo.documents` with a single column, `document`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-11T20:58:43.144688Z",
     "start_time": "2024-04-11T20:58:42.912418Z"
    },
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Created table `documents`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_81bb3 th {\n",
       "  text-align: center;\n",
       "}\n",
       "#T_81bb3_row0_col0, #T_81bb3_row0_col1, #T_81bb3_row0_col2 {\n",
       "  white-space: pre-wrap;\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_81bb3\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th id=\"T_81bb3_level0_col0\" class=\"col_heading level0 col0\" >Column Name</th>\n",
       "      <th id=\"T_81bb3_level0_col1\" class=\"col_heading level0 col1\" >Type</th>\n",
       "      <th id=\"T_81bb3_level0_col2\" class=\"col_heading level0 col2\" >Computed With</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_81bb3_row0_col0\" class=\"data row0 col0\" >document</td>\n",
       "      <td id=\"T_81bb3_row0_col1\" class=\"data row0 col1\" >document</td>\n",
       "      <td id=\"T_81bb3_row0_col2\" class=\"data row0 col2\" ></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "table 'documents'\n",
       "\n",
       "Column Name     Type Computed With\n",
       "   document document              "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents_t = pxt.create_table('rag_demo.documents', {'document': pxt.DocumentType()})\n",
    "documents_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "metadata": {}
   },
   "source": [
    "Next, we'll insert our first few source documents into the new table. We'll leave the rest for later, in order to show how to update the indexed document base incrementally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "document_urls = [\n",
    "    base + 'Argus-Market-Digest-June-2024.pdf',\n",
    "    base + 'Argus-Market-Watch-June-2024.pdf',\n",
    "    base + 'Company-Research-Alphabet.pdf',\n",
    "    base + 'Jefferson-Amazon.pdf',\n",
    "    base + 'Mclean-Equity-Alphabet.pdf',\n",
    "    base + 'Zacks-Nvidia-Repeport.pdf',\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "metadata": {},
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inserting rows into `documents`: 3 rows [00:00, 1660.45 rows/s]\n",
      "Inserted 3 rows with 0 errors.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>document</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_0f95e650ad40e78bbe8b427de7c9095a9b8fa24cf432708e6df3c69626e732be.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_a1ecd0cc068f969b047d54f4611ec5620f9b2fb92aeecb3a605106591e2e3980.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_66aa90e7cb16107a4d93664b7c4ae5f1619cf0b259780380e30f93f6127b7f02.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                            document\n",
       "0  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "1  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "2  /Users/asiegel/.pixeltable/file_cache/ae7c9d97..."
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents_t.insert({'document': url} for url in document_urls[:3])\n",
    "documents_t.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In RAG applications, we often decompose documents into smaller units, or chunks, rather than treating each document as a single entity. In this example, we'll use Pixeltable's built-in `DocumentSplitter`, but in general the chunking methodology is highly customizable. `DocumentSplitter` has a variety of options for controlling the chunking behavior, and it's also possible to replace it entirely with a user-defined iterator (or an adapter for a third-party document splitter).\n",
    "\n",
    "In Pixeltable, operations such as chunking can be automated by creating **views** of the base `documents` table. A view is a virtual derived table: rather than adding data directly to the view, we define it via a computation over the base table. In this example, the view is defined by iteration over the chunks of a `DocumentSplitter`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inserting rows into `chunks`: 41 rows [00:00, 9343.46 rows/s]\n",
      "Created view `chunks` with 41 rows, 0 exceptions.\n"
     ]
    }
   ],
   "source": [
    "from pixeltable.iterators import DocumentSplitter\n",
    "\n",
    "chunks_t = pxt.create_view(\n",
    "    'rag_demo.chunks',\n",
    "    documents_t,\n",
    "    iterator=DocumentSplitter.create(\n",
    "        document=documents_t.document,\n",
    "        separators='token_limit', limit=300)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our `chunks` view now has 3 columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_e5a4f th {\n",
       "  text-align: center;\n",
       "}\n",
       "#T_e5a4f_row0_col0, #T_e5a4f_row0_col1, #T_e5a4f_row0_col2, #T_e5a4f_row1_col0, #T_e5a4f_row1_col1, #T_e5a4f_row1_col2, #T_e5a4f_row2_col0, #T_e5a4f_row2_col1, #T_e5a4f_row2_col2 {\n",
       "  white-space: pre-wrap;\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_e5a4f\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th id=\"T_e5a4f_level0_col0\" class=\"col_heading level0 col0\" >Column Name</th>\n",
       "      <th id=\"T_e5a4f_level0_col1\" class=\"col_heading level0 col1\" >Type</th>\n",
       "      <th id=\"T_e5a4f_level0_col2\" class=\"col_heading level0 col2\" >Computed With</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_e5a4f_row0_col0\" class=\"data row0 col0\" >pos</td>\n",
       "      <td id=\"T_e5a4f_row0_col1\" class=\"data row0 col1\" >int</td>\n",
       "      <td id=\"T_e5a4f_row0_col2\" class=\"data row0 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_e5a4f_row1_col0\" class=\"data row1 col0\" >text</td>\n",
       "      <td id=\"T_e5a4f_row1_col1\" class=\"data row1 col1\" >string</td>\n",
       "      <td id=\"T_e5a4f_row1_col2\" class=\"data row1 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_e5a4f_row2_col0\" class=\"data row2 col0\" >document</td>\n",
       "      <td id=\"T_e5a4f_row2_col1\" class=\"data row2 col1\" >document</td>\n",
       "      <td id=\"T_e5a4f_row2_col2\" class=\"data row2 col2\" ></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "view 'chunks'\n",
       "\n",
       "Column Name     Type Computed With\n",
       "        pos      int              \n",
       "       text   string              \n",
       "   document document              "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chunks_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- `text` is the chunk text produced by the `DocumentSplitter`\n",
    "- `pos` is a system-generated integer column, starting at 0, that provides a sequence number for each row\n",
    "- `document`, which is simply the `document` column from the base table `documents`. We won't need it here, but having access to the base table's columns (in effect a parent-child join) can be quite useful."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that as soon as we created it, `chunks` was automatically populated with data from the existing documents in our base table. We can select the first 2 chunks from each document using common dataframe operations, in order to get a feel for what was extracted:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "metadata": {},
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>pos</th>\n",
       "      <th>text</th>\n",
       "      <th>document</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>MARKET DIGEST\n",
       "- 1 -\n",
       " FRIDAY, JUNE 21, 2024\n",
       "JUNE 20, DJIA: 39,134.76\n",
       "UP 299.90\n",
       "Independent Equity Research Since 1934\n",
       "AR\n",
       "GUS\n",
       "A R G U S  R E S E A R C H  C O M P A N Y  •  6 1  B R O A D W A Y  •  N E W  Y O R K ,  N. Y.  1 0 0 0 6  •  ( 2 1 2 )  4 2 5  -  7 5 0 0\n",
       "LONDON SALES &amp; MARKETING OFFICE TEL 011-44-207-256-8383 / FAX 011-44-207-256-8363\n",
       "®\n",
       "Good Morning. This is the Market Digest for Friday, June 21, 2024, with analysis of the financial markets and comments on\n",
       "Accenture plc.\n",
       "IN THIS ISSUE:\n",
       "*\n",
       "Growth Stock: Accenture plc: Shares rally on AI optimism (Jim Kelleher)\n",
       "MARKET REVIEW:\n",
       "Yogi Berra famously said &quot;When you come to a fork in the road, take it.&quot; Stock investors did just that on Thursday,\n",
       "pushing the Dow Jones Industrial Average higher by 0.77% but the Nasdaq Composite and</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_0f95e650ad40e78bbe8b427de7c9095a9b8fa24cf432708e6df3c69626e732be.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>S&amp;P 500 lower by 0.79% and 0.25%,\n",
       "respectively. In a rare event, shares of Nvidia not only lost ground, but lost a relatively meaningful amount (3.5%), proving nothing\n",
       "can go up forever. Still, the major indices are comfortably ahead for the year to date — and the big non-AI mover for stocks is\n",
       "the future direction of interest rates, which remains a concern for Wall Street and (for one day at least) offset AI mania.\n",
       "ACCENTURE PLC (NYSE: ACN, \\$306.16) BUY\n",
       "ACN: Shares rally on AI optimism\n",
       "*\n",
       "A ...... wing AI momentum for the business.\n",
       "*\n",
       "Total bookings were strong in 3Q24, rising 22% annually on acceleration in managed services and resulting in a\n",
       "1.3 book-to-bill. Accenture still appears to be taking share from competitors.\n",
       "*\n",
       "We believe that Accenture has the financial resources, customer presence, and market strength to thrive as\n",
       "companies accelerate the process of digital transformation and begin their AI journeys.\n",
       "ANALYSIS\n",
       "INVESTMENT THESIS\n",
       "Shares of BUY-rated Accenture plc (NYSE: ACN)</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_0f95e650ad40e78bbe8b427de7c9095a9b8fa24cf432708e6df3c69626e732be.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>+11.78%\n",
       "Enterprise Value\n",
       "\\$2103.1 B\n",
       "6/20/2024\n",
       "Ex. Dividend Date\n",
       "6/10/2024\n",
       "Dividend\n",
       "\\$0.200000\n",
       "Dividend Yield (Annualized)\n",
       "0.45%\n",
       "6/20/2024\n",
       "P/E (TTM)\n",
       "27.0\n",
       "6/20/2024\n",
       "Earning Yield (TTM)\n",
       "+3.70%\n",
       "6/20/2024\n",
       "EPS (Adjusted TTM)\n",
       "\\$1.89\n",
       "4/25/2024\n",
       "Consensus EPS Estimate\n",
       "(Q2 2024)\n",
       "\\$1.84\n",
       "EPS Growth\n",
       "(TTM vs. Prior TTM)\n",
       "+45.2%\n",
       "3-Year Price Performance  Data provided by DataScope from Refinitiv\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "2021\n",
       "2022\n",
       "2023\n",
       "2024\n",
       "-40%\n",
       "-20%\n",
       "0%\n",
       "20%\n",
       "40%\n",
       "0\n",
       "500\n",
       "Average Monthly Volume (Millions)\n",
       "50-Day Moving Average\n",
       "200-Day Moving Average\n",
       "Trading Characteristics\n",
       "52 Week High\n",
       "6/12/2024\n",
       "\\$180.41\n",
       "52 Week Low\n",
       "7/11/2023\n",
       "\\$115.35\n",
       "% Price Above/Below\n",
       "   20-Day Average\n",
       "2.4\n",
       "   50-Day Average</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_66aa90e7cb16107a4d93664b7c4ae5f1619cf0b259780380e30f93f6127b7f02.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Friday, June 21, 2024\n",
       "Intermediate Term:\n",
       "Market Outlook\n",
       "Bullish\n",
       "-------------- PORTFOLIO STRATEGY -------------\n",
       "Equity:\n",
       "72%\n",
       "Cash:\n",
       "1%\n",
       "Today&#x27;s Market Movers\n",
       "IMPACT\n",
       "aGlobal Shares Lower\n",
       "GILD Pops on HIV Drug Results\n",
       "SRPT Soars on FDA Approval\n",
       "SWBI Drops on Sales Guidance\n",
       "+\n",
       "+\n",
       "+\n",
       "-\n",
       "a\n",
       "a\n",
       "a\n",
       "Recent Research Review\n",
       "ADSK, MRNA, IQV, WMB, BUD, LYFT, SRE, BP, \n",
       "AEE, PPC, JNPR, ORCL, CMG, TPR, DPZ, EOG, \n",
       "COST, PLTR, COR, VRTX\n",
       "Statistics Diary\n",
       "12-Mth S&amp;P 500 Forcast:\n",
       "S&amp;P 500 Current/Next EPS:\n",
       "S&amp;P 500 P/E:\n",
       "12-Mth S&amp;P P/E Range:\n",
       "10-Year Yield:\n",
       "12-Mth 10-Yr. Bond Forecast:\n",
       "Current Fed Funds Target:\n",
       "12-Mth Fed Funds Forecast:\n",
       "4800-5600\n",
       "247/265\n",
       "22.16\n",
       "18.1 - 21.1\n",
       "4.26%\n",
       "3.50-4.50%\n",
       "4.62%\n",
       "4.50-5.50%\n",
       "DJIA:\n",
       "S&amp;P 500:\n",
       "NASDAQ:\n",
       "Lrg/Small Cap:\n",
       "Growth/Value:\n",
       "PREVIOUS\n",
       "CLOSE\n",
       "200-DAY\n",
       "AVERAGE\n",
       "39134.76</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_a1ecd0cc068f969b047d54f4611ec5620f9b2fb92aeecb3a605106591e2e3980.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>37058.23\n",
       "5473.17\n",
       "4831.39\n",
       "17721.59\n",
       "15160.55\n",
       "1.48\n",
       "1.37\n",
       "2.07\n",
       "1.86\n",
       "CURRENT\n",
       "RANKING\n",
       "Five-Day Put/Call:\n",
       "Momentum:\n",
       "Bullish Sentiment:\n",
       "Mutual Fund Cash:\n",
       "Vickers Insider Index:\n",
       "1.00\n",
       "Positive\n",
       "346000\n",
       "Neutral\n",
       "44%\n",
       "Positive\n",
       "1.70%\n",
       "Negative\n",
       "3.42\n",
       "Negative\n",
       "Housing Sentiment Slumps\n",
       "Mortgage rates near 7% are pushing prospective buyers to the sidelines and \n",
       "could turn housing to a drag on 2Q GDP after a strong contribution to 1Q \n",
       "growth. &quot;Millions of potential homebuyers have been priced out of the market \n",
       "by  ...... rding to The State of The \n",
       "Nation&#x27;s Housing report, which was published yesterday by Harvard&#x27;s Joint \n",
       "Center for Housing Studies. Fannie Mae&#x27;s Home Purchase Sentiment Index \n",
       "for May dropped by 2.5 points to an all-time survey low of 69.4. Just 14% of \n",
       "consumers said that it is a good time to buy a home, down from 20% in April. \n",
       "Doug \n",
       "Duncan, \n",
       "Chief \n",
       "Economist \n",
       "at \n",
       "Fannie \n",
       "Mae, \n",
       "said \n",
       "&quot;While \n",
       "many \n",
       "respondents expressed optimism at the beginning of the year that mortgage \n",
       "rates would decline,</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_a1ecd0cc068f969b047d54f4611ec5620f9b2fb92aeecb3a605106591e2e3980.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Company Research Highlights\n",
       "®\n",
       "Report created on June 21, 2024\n",
       "This is not an investment recommendation from Fidelity Investments. Fidelity provides this information as a service to investors from independent, third-party sources.\n",
       "Performance of analyst recommendations are provided by StarMine from Refinitiv. Current analyst recommendations are collected and standardized by Investars.\n",
       "See each section in this report for third-party content attribution, as well as the final page of the report  ...... Market Capitalization:\n",
       "\\$2182.8 B\n",
       "Interactive Media &amp; Services Industry\n",
       "Business Description  Data provided by S&amp;P Compustat\n",
       "Alphabet Inc. offers various products and platforms in the United States, Europe, the Middle\n",
       "East, Africa, the Asia-Pacific, Canada, and Latin America. It operates through Google Services,\n",
       "Google Cloud, and Other Bets segments.\n",
       "Key Statistics\n",
       "Employee Count\n",
       "182,502\n",
       "Institutional Ownership\n",
       "80.9%\n",
       "Total Revenue (TTM)\n",
       "\\$80,539.00\n",
       "3/31/2024\n",
       "Revenue Growth\n",
       "(TTM vs. Prior TTM)</td>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_66aa90e7cb16107a4d93664b7c4ae5f1619cf0b259780380e30f93f6127b7f02.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "   pos                                               text  \\\n",
       "0    0  MARKET DIGEST\\n- 1 -\\n FRIDAY, JUNE 21, 2024\\n...   \n",
       "1    1   S&P 500 lower by 0.79% and 0.25%,\\nrespective...   \n",
       "2    1  +11.78%\\nEnterprise Value\\n$2103.1 B\\n6/20/202...   \n",
       "3    0  Friday, June 21, 2024\\nIntermediate Term:\\nMar...   \n",
       "4    1  \\n37058.23\\n5473.17\\n4831.39\\n17721.59\\n15160....   \n",
       "5    0  Company Research Highlights\\n®\\nReport created...   \n",
       "\n",
       "                                            document  \n",
       "0  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  \n",
       "1  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  \n",
       "2  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  \n",
       "3  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  \n",
       "4  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  \n",
       "5  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chunks_t.where(chunks_t.pos < 2).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's compute vector embeddings for the document chunks and store them in a vector index. Pixeltable has built-in support for vector indexing using a variety of embedding model families, and it's easy for users to add new ones via UDFs. In this demo, we're going to use the E5 model from the Huggingface `sentence_transformers` library, which runs locally. \n",
    "\n",
    "The following command creates a vector index on the `text` column in the `chunks` table, using the E5 embedding model. Note that defining the index is sufficient in order to load it with the existing data (and also to update it when the underlying data changes, as we'll see later)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|██████████████████████████████████████████| 41/41 [00:11<00:00,  3.69 cells/s]\n"
     ]
    }
   ],
   "source": [
    "from pixeltable.functions.huggingface import sentence_transformer\n",
    "\n",
    "@pxt.expr_udf\n",
    "def e5_embed(text: str) -> np.ndarray:\n",
    "    return sentence_transformer(text, model_id='intfloat/e5-large-v2')\n",
    "\n",
    "chunks_t.add_embedding_index('text', string_embed=e5_embed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This completes the first part of our application, creating an indexed document base. Next, we'll use it to run some queries."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Querying\n",
    "\n",
    "In order to express a top-k lookup against our index, we use Pixeltable's `similarity` operator in combination with the standard `order_by` and `limit` operations. Before building this into our application, let's run a sample query to make sure it works."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "metadata": {},
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>similarity</th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0.801</td>\n",
       "      <td>on 6/20/2024:\n",
       "\\$176.30\n",
       "Communication Services Sector\n",
       "Market Capitalization:\n",
       "\\$2182.8 B\n",
       "Interactive Media &amp; Services Industry\n",
       "Alphabet Class A\n",
       "The content on this page is provided by third parties and not Fidelity. Fidelity did not prepare and does not endorse such content. All are third-party companies that are not affiliated with Fidelity.\n",
       "See each section in this report for third-party content attribution and see page 4 for full disclosures.\n",
       "Page 2\n",
       "Report created on June 21, 2024\n",
       "Equity Sum ...... rom\n",
       "contributors. The number of contributors for\n",
       "each security where there is a consensus\n",
       "recommendation is provided. Each\n",
       "contributor determines how their individual\n",
       "recommendation scale maps to the I/B/E/S\n",
       "from Refinitiv 5-point scale.\n",
       "Visit Fidelity.com for firm descriptions,\n",
       "detailed methodologies, and more\n",
       "information on the Equity Summary Score,\n",
       "First Call Consensus, opinion history and\n",
       "performance, and most current available\n",
       "research reports for GOOGL.\n",
       "Equity Summary Score  (7 Firms†)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.799</td>\n",
       "      <td>/B/E/S from Refinitiv\n",
       "Earnings in US Dollars\n",
       "ACTUALS\n",
       "ESTIMATES\n",
       "STARMINE ESTIMATES\n",
       "GOOGL PRICE\n",
       "Q2\n",
       "Q1\n",
       "2024\n",
       "2024\n",
       "2024\n",
       "2024\n",
       "Q4\n",
       "Q3\n",
       "Q2\n",
       "Q1\n",
       "2023\n",
       "2023\n",
       "2023\n",
       "2023\n",
       "Q4\n",
       "Q3\n",
       "Q2\n",
       "Q1\n",
       "2022\n",
       "2022\n",
       "2022\n",
       "2022\n",
       "\\$0\n",
       "\\$100\n",
       "\\$200\n",
       "Today\n",
       "1.23\n",
       "1.21\n",
       "1.06\n",
       "1.05\n",
       "1.17\n",
       "1.44\n",
       "1.55\n",
       "1.64\n",
       "1.89\n",
       "1.84\n",
       "Actuals vs. Estimates for Fiscal Year\n",
       "First Call Estimates\n",
       " \n",
       "Actual (\\$)\n",
       "Consensus (\\$)\n",
       "Analysts in Consensus\n",
       "Low / High Range (\\$)\n",
       "Previous Year (Ends 12/31/23)\n",
       "5.80\n",
       "5.74\n",
       "48\n",
       "5.26 / 6.10\n",
       "Next Year (Ends 12/31/24)\n",
       "--\n",
       "7.57\n",
       "53\n",
       "6.87 / 8.17\n",
       "Industry Comparisons**  Data provided by S&amp;P Compustat\n",
       "Valuation Ratios (trailing 12 months)\n",
       "GOOGL\n",
       "Industry\n",
       "GOOGL Percentile Rank in Industry\n",
       "Price / Earnings\n",
       "27.0\n",
       "28.1\n",
       "--\n",
       "PEG Ratio\n",
       "1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.796</td>\n",
       "      <td>+11.78%\n",
       "Enterprise Value\n",
       "\\$2103.1 B\n",
       "6/20/2024\n",
       "Ex. Dividend Date\n",
       "6/10/2024\n",
       "Dividend\n",
       "\\$0.200000\n",
       "Dividend Yield (Annualized)\n",
       "0.45%\n",
       "6/20/2024\n",
       "P/E (TTM)\n",
       "27.0\n",
       "6/20/2024\n",
       "Earning Yield (TTM)\n",
       "+3.70%\n",
       "6/20/2024\n",
       "EPS (Adjusted TTM)\n",
       "\\$1.89\n",
       "4/25/2024\n",
       "Consensus EPS Estimate\n",
       "(Q2 2024)\n",
       "\\$1.84\n",
       "EPS Growth\n",
       "(TTM vs. Prior TTM)\n",
       "+45.2%\n",
       "3-Year Price Performance  Data provided by DataScope from Refinitiv\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "Q3\n",
       "Q4\n",
       "Q1\n",
       "Q2\n",
       "2021\n",
       "2022\n",
       "2023\n",
       "2024\n",
       "-40%\n",
       "-20%\n",
       "0%\n",
       "20%\n",
       "40%\n",
       "0\n",
       "500\n",
       "Average Monthly Volume (Millions)\n",
       "50-Day Moving Average\n",
       "200-Day Moving Average\n",
       "Trading Characteristics\n",
       "52 Week High\n",
       "6/12/2024\n",
       "\\$180.41\n",
       "52 Week Low\n",
       "7/11/2023\n",
       "\\$115.35\n",
       "% Price Above/Below\n",
       "   20-Day Average\n",
       "2.4\n",
       "   50-Day Average</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.795</td>\n",
       "      <td>Friday, June 21, 2024\n",
       "Intermediate Term:\n",
       "Market Outlook\n",
       "Bullish\n",
       "-------------- PORTFOLIO STRATEGY -------------\n",
       "Equity:\n",
       "72%\n",
       "Cash:\n",
       "1%\n",
       "Today&#x27;s Market Movers\n",
       "IMPACT\n",
       "aGlobal Shares Lower\n",
       "GILD Pops on HIV Drug Results\n",
       "SRPT Soars on FDA Approval\n",
       "SWBI Drops on Sales Guidance\n",
       "+\n",
       "+\n",
       "+\n",
       "-\n",
       "a\n",
       "a\n",
       "a\n",
       "Recent Research Review\n",
       "ADSK, MRNA, IQV, WMB, BUD, LYFT, SRE, BP, \n",
       "AEE, PPC, JNPR, ORCL, CMG, TPR, DPZ, EOG, \n",
       "COST, PLTR, COR, VRTX\n",
       "Statistics Diary\n",
       "12-Mth S&amp;P 500 Forcast:\n",
       "S&amp;P 500 Current/Next EPS:\n",
       "S&amp;P 500 P/E:\n",
       "12-Mth S&amp;P P/E Range:\n",
       "10-Year Yield:\n",
       "12-Mth 10-Yr. Bond Forecast:\n",
       "Current Fed Funds Target:\n",
       "12-Mth Fed Funds Forecast:\n",
       "4800-5600\n",
       "247/265\n",
       "22.16\n",
       "18.1 - 21.1\n",
       "4.26%\n",
       "3.50-4.50%\n",
       "4.62%\n",
       "4.50-5.50%\n",
       "DJIA:\n",
       "S&amp;P 500:\n",
       "NASDAQ:\n",
       "Lrg/Small Cap:\n",
       "Growth/Value:\n",
       "PREVIOUS\n",
       "CLOSE\n",
       "200-DAY\n",
       "AVERAGE\n",
       "39134.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.794</td>\n",
       "      <td>2024:\n",
       "\\$176.30\n",
       "Communication Services Sector\n",
       "Market Capitalization:\n",
       "\\$2182.8 B\n",
       "Interactive Media &amp; Services Industry\n",
       "Alphabet Class A\n",
       "The content on this page is provided by third parties and not Fidelity. Fidelity did not prepare and does not endorse such content. All are third-party companies that are not affiliated with Fidelity.\n",
       "See each section in this report for third-party content attribution.\n",
       " \n",
       " © 2024 FMR LLC. All rights reserved. 447628.8.0\n",
       "Page 4\n",
       "Report created on June 21, 2024\n",
       "Impo ...... alyst recommendations data and analysis provided by Investars. All are third-party companies that are not affiliated with Fidelity.\n",
       "This report provides quotes and data concerning the financial markets, securities and other subjects. Content is supplied by companies that are not affiliated with Fidelity (&quot;Third-Party\n",
       "Content&quot;). Most Third-Party Content and its source are clearly and prominently identified. Fidelity does not prepare, edit, or endorse Third-Party Content. News and research are</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "   similarity                                               text\n",
       "0    0.800725   on 6/20/2024:\\n$176.30\\nCommunication Service...\n",
       "1    0.799386  /B/E/S from Refinitiv\\nEarnings in US Dollars\\...\n",
       "2    0.796000  +11.78%\\nEnterprise Value\\n$2103.1 B\\n6/20/202...\n",
       "3    0.794835  Friday, June 21, 2024\\nIntermediate Term:\\nMar...\n",
       "4    0.794355  2024:\\n$176.30\\nCommunication Services Sector\\..."
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_text = \"What is the expected EPS for Nvidia in Q1 2026?\"\n",
    "sim = chunks_t.text.similarity(query_text)\n",
    "nvidia_eps_query = (\n",
    "    chunks_t.order_by(sim, asc=False)\n",
    "        .select(similarity=sim, text=chunks_t.text)\n",
    "        .limit(5)\n",
    "    )\n",
    "nvidia_eps_query.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We perform this context retrieval for each row of our `queries` table by adding it as a computed column. In this case, the operation is a top-k similarity lookup against the data in the `chunks` table. To implement this operation, we'll use Pixeltable's `@query` decorator to enhance the capabilities of the `chunks` table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|████████████████████████████████████████████| 8/8 [00:02<00:00,  3.97 cells/s]\n",
      "Added 8 column values with 0 errors.\n"
     ]
    }
   ],
   "source": [
    "# A @query is essentially a reusable, parameterized query that is attached to a table (or view),\n",
    "# which is a modular way of getting data from that table.\n",
    "\n",
    "@chunks_t.query\n",
    "def top_k(query_text: str):\n",
    "    sim = chunks_t.text.similarity(query_text)\n",
    "    return (\n",
    "        chunks_t.order_by(sim, asc=False)\n",
    "            .select(chunks_t.text, sim=sim)\n",
    "            .limit(5)\n",
    "    )\n",
    "\n",
    "# Now add a computed column to `queries_t`, calling the stored query\n",
    "# `chunks_t.top_k` that we just defined.\n",
    "queries_t['question_context'] = chunks_t.top_k(queries_t.Question)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our `queries` table now looks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_83ef4 th {\n",
       "  text-align: center;\n",
       "}\n",
       "#T_83ef4_row0_col0, #T_83ef4_row0_col1, #T_83ef4_row0_col2, #T_83ef4_row1_col0, #T_83ef4_row1_col1, #T_83ef4_row1_col2, #T_83ef4_row2_col0, #T_83ef4_row2_col1, #T_83ef4_row2_col2, #T_83ef4_row3_col0, #T_83ef4_row3_col1, #T_83ef4_row3_col2 {\n",
       "  white-space: pre-wrap;\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_83ef4\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th id=\"T_83ef4_level0_col0\" class=\"col_heading level0 col0\" >Column Name</th>\n",
       "      <th id=\"T_83ef4_level0_col1\" class=\"col_heading level0 col1\" >Type</th>\n",
       "      <th id=\"T_83ef4_level0_col2\" class=\"col_heading level0 col2\" >Computed With</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_83ef4_row0_col0\" class=\"data row0 col0\" >S__No_</td>\n",
       "      <td id=\"T_83ef4_row0_col1\" class=\"data row0 col1\" >int</td>\n",
       "      <td id=\"T_83ef4_row0_col2\" class=\"data row0 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_83ef4_row1_col0\" class=\"data row1 col0\" >Question</td>\n",
       "      <td id=\"T_83ef4_row1_col1\" class=\"data row1 col1\" >string</td>\n",
       "      <td id=\"T_83ef4_row1_col2\" class=\"data row1 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_83ef4_row2_col0\" class=\"data row2 col0\" >correct_answer</td>\n",
       "      <td id=\"T_83ef4_row2_col1\" class=\"data row2 col1\" >string</td>\n",
       "      <td id=\"T_83ef4_row2_col2\" class=\"data row2 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_83ef4_row3_col0\" class=\"data row3 col0\" >question_context</td>\n",
       "      <td id=\"T_83ef4_row3_col1\" class=\"data row3 col1\" >json</td>\n",
       "      <td id=\"T_83ef4_row3_col2\" class=\"data row3 col2\" >top_k(Question)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "table 'queries'\n",
       "\n",
       "     Column Name   Type   Computed With\n",
       "          S__No_    int                \n",
       "        Question string                \n",
       "  correct_answer string                \n",
       "question_context   json top_k(Question)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The new column `question_context` now contains the result of executing the query for each row, formatted as a list of dictionaries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>question_context</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>[{&quot;sim&quot;: 0.795, &quot;text&quot;: &quot; that simply hasn&#x27;t happened, and current sentiment \\nreflects pent-up frustration with the overall lack of purchase affordability.\\&quot; \\nBased on the  ...... .5% for April. The Zillow Home Value index rose by 4.3% in April and \\n4.3% in May. High mortgage rates are a challenge, but we remain bullish on \\n&quot;}, {&quot;sim&quot;: 0.794, &quot;text&quot;: &quot;\\n37058.23\\n5473.17\\n4831.39\\n17721.59\\n15160.55\\n1.48\\n1.37\\n2.07\\n1.86\\nCURRENT\\nRANKING\\nFive-Day Put/Call:\\nMomentum:\\nBullish Sentiment:\\nMutual Fund Cash:\\n ......  \\nEconomist \\nat \\nFannie \\nMae, \\nsaid \\n\\&quot;While \\nmany \\nrespondents expressed optimism at the beginning of the year that mortgage \\nrates would decline,&quot;}, {&quot;sim&quot;: 0.779, &quot;text&quot;: &quot;+11.78%\\nEnterprise Value\\n\\$2103.1 B\\n6/20/2024\\nEx. Dividend Date\\n6/10/2024\\nDividend\\n\\$0.200000\\nDividend Yield (Annualized)\\n0.45%\\n6/20/2024\\nP/E (TTM)\\n ...... e\\nTrading Characteristics\\n52 Week High\\n6/12/2024\\n\\$180.41\\n52 Week Low\\n7/11/2023\\n\\$115.35\\n% Price Above/Below\\n   20-Day Average\\n2.4\\n   50-Day Average&quot;}, {&quot;sim&quot;: 0.77, &quot;text&quot;: &quot;, duplication, redistribution or disclosure is prohibited by law and can result in prosecution. The content of this report \\nmay be derived from Ar ......  no liability for any loss arising from the use of this report, nor shall Argus treat all recipients of this report as \\ncustomers simply by virtue&quot;}, {&quot;sim&quot;: 0.768, &quot;text&quot;: &quot; \\$11.97-\\$12.20; the new guidance implies growth of 2%-3% from FY23. That is down from 3%-5% growth\\nguidance offered in March 2024.\\nWe are reducing ...... h flow from operations was \\$9.52 billion in FY23, \\$9.54 billion in FY22, \\$8.98 billion in FY21, \\$8.36 billion in FY20,\\nand \\$6.63 billion in FY19.\\n&quot;}]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                    question_context\n",
       "0  [{'sim': 0.7950694683152768, 'text': ' that si..."
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t.select(queries_t.question_context).head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Asking the LLM\n",
    "\n",
    "Now it's time for the final step in our application: feeding the document chunks and questions to an LLM for resolution. In this demo, we'll use OpenAI for this, but any other inference cloud or local model could be used instead.\n",
    "\n",
    "We start by defining a UDF that takes a top-k list of context chunks and a question and turns them into a ChatGPT prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define a UDF to create an LLM prompt given a top-k list of context chunks and a question.\n",
    "@pxt.udf\n",
    "def create_prompt(top_k_list: list[dict], question: str) -> str:\n",
    "    concat_top_k = '\\n\\n'.join(elt['text'] for elt in reversed(top_k_list))\n",
    "    return f'''\n",
    "    PASSAGES:\n",
    "\n",
    "    {concat_top_k}\n",
    "\n",
    "    QUESTION:\n",
    "\n",
    "    {question}'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then add that again as a computed column to `queries`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|███████████████████████████████████████████| 8/8 [00:00<00:00, 458.80 cells/s]\n",
      "Computing cells: 100%|██████████████████████████████████████████| 8/8 [00:00<00:00, 1213.02 cells/s]\n",
      "Added 8 column values with 0 errors.\n"
     ]
    }
   ],
   "source": [
    "queries_t['prompt'] = create_prompt(queries_t.question_context, queries_t.Question)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have a new string column containing the prompt:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_db246 th {\n",
       "  text-align: center;\n",
       "}\n",
       "#T_db246_row0_col0, #T_db246_row0_col1, #T_db246_row0_col2, #T_db246_row1_col0, #T_db246_row1_col1, #T_db246_row1_col2, #T_db246_row2_col0, #T_db246_row2_col1, #T_db246_row2_col2, #T_db246_row3_col0, #T_db246_row3_col1, #T_db246_row3_col2, #T_db246_row4_col0, #T_db246_row4_col1, #T_db246_row4_col2 {\n",
       "  white-space: pre-wrap;\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_db246\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th id=\"T_db246_level0_col0\" class=\"col_heading level0 col0\" >Column Name</th>\n",
       "      <th id=\"T_db246_level0_col1\" class=\"col_heading level0 col1\" >Type</th>\n",
       "      <th id=\"T_db246_level0_col2\" class=\"col_heading level0 col2\" >Computed With</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_db246_row0_col0\" class=\"data row0 col0\" >S__No_</td>\n",
       "      <td id=\"T_db246_row0_col1\" class=\"data row0 col1\" >int</td>\n",
       "      <td id=\"T_db246_row0_col2\" class=\"data row0 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_db246_row1_col0\" class=\"data row1 col0\" >Question</td>\n",
       "      <td id=\"T_db246_row1_col1\" class=\"data row1 col1\" >string</td>\n",
       "      <td id=\"T_db246_row1_col2\" class=\"data row1 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_db246_row2_col0\" class=\"data row2 col0\" >correct_answer</td>\n",
       "      <td id=\"T_db246_row2_col1\" class=\"data row2 col1\" >string</td>\n",
       "      <td id=\"T_db246_row2_col2\" class=\"data row2 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_db246_row3_col0\" class=\"data row3 col0\" >question_context</td>\n",
       "      <td id=\"T_db246_row3_col1\" class=\"data row3 col1\" >json</td>\n",
       "      <td id=\"T_db246_row3_col2\" class=\"data row3 col2\" >top_k(Question)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_db246_row4_col0\" class=\"data row4 col0\" >prompt</td>\n",
       "      <td id=\"T_db246_row4_col1\" class=\"data row4 col1\" >string</td>\n",
       "      <td id=\"T_db246_row4_col2\" class=\"data row4 col2\" >create_prompt(question_context, Question)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "table 'queries'\n",
       "\n",
       "     Column Name   Type                             Computed With\n",
       "          S__No_    int                                          \n",
       "        Question string                                          \n",
       "  correct_answer string                                          \n",
       "question_context   json                           top_k(Question)\n",
       "          prompt string create_prompt(question_context, Question)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>prompt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>PASSAGES:\n",
       "\n",
       "     \\$11.97-\\$12.20; the new guidance implies growth of 2%-3% from FY23. That is down from 3%-5% growth\n",
       "guidance offered in March 2024.\n",
       "We are reducing our FY24 non-GAAP earnings estimate to \\$11.88 per diluted share from \\$12.11. However, we are\n",
       "raising our FY25 forecast to \\$12.70 per diluted share from \\$12.68. Our long-term annualized EPS growth rate forecast is 10%.\n",
       "FINANCIAL STRENGTH &amp; DIVIDEND\n",
       "Accenture&#x27;s financial strength ranking is High. The company, which formerly had f ...... of 4.10 million (SAAR), down from 4.23 million in May 2023. Next \n",
       "week, we expect the Commerce Department to report May New Home Sales \n",
       "of 640,000 (SAAR), down from 741,000 a year earlier. The S&amp;P/Case-Shiller \n",
       "National Home Price Index jumped 6.5% in March. We expect it to rise about \n",
       "4.5% for April. The Zillow Home Value index rose by 4.3% in April and \n",
       "4.3% in May. High mortgage rates are a challenge, but we remain bullish on \n",
       "\n",
       "\n",
       "    QUESTION:\n",
       "\n",
       "    What is roughly the current mortage rate?</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                              prompt\n",
       "0  \\n    PASSAGES:\\n\\n     $11.97-$12.20; the new..."
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t.select(queries_t.prompt).head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now add another computed column to call OpenAI. For the `chat_completions()` call, we need to construct two messages, containing the instructions to the model and the prompt. For the latter, we can simply reference the `prompt` column we just added."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|████████████████████████████████████████████| 8/8 [00:10<00:00,  1.28s/ cells]\n",
      "Added 8 column values with 0 errors.\n"
     ]
    }
   ],
   "source": [
    "from pixeltable.functions import openai\n",
    "\n",
    "# Assemble the prompt and instructions into OpenAI's message format\n",
    "messages = [\n",
    "    {'role': 'system', 'content': 'Please read the following passages and answer the question based on their contents.'},\n",
    "    {'role': 'user', 'content': queries_t.prompt}\n",
    "]\n",
    "\n",
    "# Add a computed column that calls OpenAI\n",
    "queries_t['response'] = openai.chat_completions(model='gpt-4o-mini', messages=messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our `queries` table now contains a JSON-structured column `response`, which holds the entire API response structure. At the moment, we're only interested in the response content, which we can extract easily into another computed column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|███████████████████████████████████████████| 8/8 [00:00<00:00, 576.79 cells/s]\n",
      "Added 8 column values with 0 errors.\n"
     ]
    }
   ],
   "source": [
    "queries_t['answer'] = queries_t.response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have the following `queries` schema:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_90a48 th {\n",
       "  text-align: center;\n",
       "}\n",
       "#T_90a48_row0_col0, #T_90a48_row0_col1, #T_90a48_row0_col2, #T_90a48_row1_col0, #T_90a48_row1_col1, #T_90a48_row1_col2, #T_90a48_row2_col0, #T_90a48_row2_col1, #T_90a48_row2_col2, #T_90a48_row3_col0, #T_90a48_row3_col1, #T_90a48_row3_col2, #T_90a48_row4_col0, #T_90a48_row4_col1, #T_90a48_row4_col2, #T_90a48_row5_col0, #T_90a48_row5_col1, #T_90a48_row5_col2, #T_90a48_row6_col0, #T_90a48_row6_col1, #T_90a48_row6_col2 {\n",
       "  white-space: pre-wrap;\n",
       "  text-align: left;\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_90a48\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th id=\"T_90a48_level0_col0\" class=\"col_heading level0 col0\" >Column Name</th>\n",
       "      <th id=\"T_90a48_level0_col1\" class=\"col_heading level0 col1\" >Type</th>\n",
       "      <th id=\"T_90a48_level0_col2\" class=\"col_heading level0 col2\" >Computed With</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row0_col0\" class=\"data row0 col0\" >S__No_</td>\n",
       "      <td id=\"T_90a48_row0_col1\" class=\"data row0 col1\" >int</td>\n",
       "      <td id=\"T_90a48_row0_col2\" class=\"data row0 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row1_col0\" class=\"data row1 col0\" >Question</td>\n",
       "      <td id=\"T_90a48_row1_col1\" class=\"data row1 col1\" >string</td>\n",
       "      <td id=\"T_90a48_row1_col2\" class=\"data row1 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row2_col0\" class=\"data row2 col0\" >correct_answer</td>\n",
       "      <td id=\"T_90a48_row2_col1\" class=\"data row2 col1\" >string</td>\n",
       "      <td id=\"T_90a48_row2_col2\" class=\"data row2 col2\" ></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row3_col0\" class=\"data row3 col0\" >question_context</td>\n",
       "      <td id=\"T_90a48_row3_col1\" class=\"data row3 col1\" >json</td>\n",
       "      <td id=\"T_90a48_row3_col2\" class=\"data row3 col2\" >top_k(Question)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row4_col0\" class=\"data row4 col0\" >prompt</td>\n",
       "      <td id=\"T_90a48_row4_col1\" class=\"data row4 col1\" >string</td>\n",
       "      <td id=\"T_90a48_row4_col2\" class=\"data row4 col2\" >create_prompt(question_context, Question)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row5_col0\" class=\"data row5 col0\" >response</td>\n",
       "      <td id=\"T_90a48_row5_col1\" class=\"data row5 col1\" >json</td>\n",
       "      <td id=\"T_90a48_row5_col2\" class=\"data row5 col2\" >chat_completions([{'role': 'system', 'content': 'Please read the following passages and answer the question based on their contents.'}, {'role': 'user', 'content': prompt}], model='gpt-4o-mini')</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td id=\"T_90a48_row6_col0\" class=\"data row6 col0\" >answer</td>\n",
       "      <td id=\"T_90a48_row6_col1\" class=\"data row6 col1\" >json</td>\n",
       "      <td id=\"T_90a48_row6_col2\" class=\"data row6 col2\" >response.choices[0].message.content</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "table 'queries'\n",
       "\n",
       "     Column Name   Type                                                                                                                                                                                      Computed With\n",
       "          S__No_    int                                                                                                                                                                                                   \n",
       "        Question string                                                                                                                                                                                                   \n",
       "  correct_answer string                                                                                                                                                                                                   \n",
       "question_context   json                                                                                                                                                                                    top_k(Question)\n",
       "          prompt string                                                                                                                                                          create_prompt(question_context, Question)\n",
       "        response   json chat_completions([{'role': 'system', 'content': 'Please read the following passages and answer the question based on their contents.'}, {'role': 'user', 'content': prompt}], model='gpt-4o-mini')\n",
       "          answer   json                                                                                                                                                                response.choices[0].message.content"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's take a look at what we got back:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Question</th>\n",
       "      <th>correct_answer</th>\n",
       "      <th>answer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>What is the expected EPS for Nvidia in Q1 2026?</td>\n",
       "      <td>0.73 EPS</td>\n",
       "      <td>The provided passages do not contain any information regarding Nvidia&#x27;s expected EPS for Q1 2026. They focus primarily on data related to Alphabet Class A (GOOGL) and certain market statistics, without mentioning Nvidia or its financial metrics. Therefore, it is not possible to answer the question based on the given information.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the market capitalization of Alphabet?</td>\n",
       "      <td>\\$2182.8 Billion</td>\n",
       "      <td>The market capitalization of Alphabet (Class A) is \\$2,182.8 billion.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the overall latest rating for Amazon.com from analysts?</td>\n",
       "      <td>SELL</td>\n",
       "      <td>The provided passages contain no information about Amazon.com; they focus solely on Alphabet Inc. (GOOGL) and its ratings from various analysts. Therefore, I cannot provide the latest rating for Amazon.com based on the information given.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is roughly the current mortage rate?</td>\n",
       "      <td>0.07</td>\n",
       "      <td>The current mortgage rate is near 7%.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the operating cash flow of Amazon in Q1 2024?</td>\n",
       "      <td>18,989 Million</td>\n",
       "      <td>The passages provided do not contain any information about Amazon&#x27;s operating cash flow for Q1 2024. They focus on financial data related to Accenture and Alphabet Class A (Google). Therefore, I cannot answer the question regarding Amazon&#x27;s operating cash flow.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What are the latest financial metrics for Accenture PLC?</td>\n",
       "      <td>missed consensus forecasts and strong total bookings rising by 22% annually</td>\n",
       "      <td>The latest financial metrics for Accenture PLC include:\n",
       "\n",
       "1. **Free Cash Flow**: \n",
       "   - Forecast for FY24: \\$8.7-\\$9.3 billion\n",
       "   - FY23: \\$9.0 billion\n",
       "   - FY22: \\$8.2 billion\n",
       "   - FY21: \\$8.4 billion\n",
       "   - FY20: \\$7.6 billion\n",
       "\n",
       "2. **Shareholder Returns**:\n",
       "   - Expected return for FY24: At least \\$7.7 billion\n",
       "   - FY23: \\$7.2 billion returned\n",
       "   - FY22: \\$6.1 billion returned\n",
       "   - FY21: \\$5.9 billion returned\n",
       "   - FY20: \\$5.0 billion returned\n",
       "\n",
       "3. **Dividends**:\n",
       "   - Quarterly dividend as of September 2023 ...... .\n",
       "   - FY25 forecast EPS: \\$12.70 per diluted share.\n",
       "\n",
       "6. **Financial Strength**:\n",
       "   - Cash at the end of 3Q24: \\$5.54 billion\n",
       "   - Debt as of the end of 3Q24: \\$1.68 billion\n",
       "   - Credit ratings: Moody&#x27;s Aa3, Standard &amp; Poor&#x27;s A+\n",
       "\n",
       "7. **Cash Flow from Operations**:\n",
       "   - FY23: \\$9.52 billion\n",
       "   - FY22: \\$9.54 billion\n",
       "\n",
       "These metrics indicate a stable financial position for Accenture with strong cash flow, significant shareholder returns, and consistent dividend growth despite macroeconomic pressures.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What are the main reasons to buy Nvidia?</td>\n",
       "      <td>Datacenter, GPUs Demands, Self-driving, and cash-flow</td>\n",
       "      <td>The passage does not provide specific reasons to buy Nvidia directly; however, it does mention a critical context regarding Nvidia&#x27;s performance. The passage states that Nvidia shares lost ground, indicating a notable decline (3.5%). This suggests that there may be market fluctuations impacting investor sentiment.\n",
       "\n",
       "Nonetheless, despite the short-term decline, the reference to &quot;the major indices are comfortably ahead for the year to date&quot; implies that Nvidia could still be part of a larger uptrend in the market. Investors might consider Nvidia as a potential buy based on its previous performance and future prospects in the AI sector, since the overall market environment remains optimistic regarding AI technologies.\n",
       "\n",
       "In summary, while the passage does not explicitly outline reasons to buy Nvidia, potential investors might focus on its involvement in AI technologies, historical performance, and the broader positive trends in the market despite recent dips.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the current dividend yield for Alphabet Inc. (\\$GOOGL)?</td>\n",
       "      <td>0.0046</td>\n",
       "      <td>The passages provided do not mention a current dividend yield for Alphabet Inc. (\\$GOOGL). In fact, there is no reference to any dividends in the content. Therefore, based on the information available, it can be concluded that Alphabet Inc. does not currently offer a dividend yield.</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                            Question  \\\n",
       "0    What is the expected EPS for Nvidia in Q1 2026?   \n",
       "1     What is the market capitalization of Alphabet?   \n",
       "2  What is the overall latest rating for Amazon.c...   \n",
       "3          What is roughly the current mortage rate?   \n",
       "4  What is the operating cash flow of Amazon in Q...   \n",
       "5  What are the latest financial metrics for Acce...   \n",
       "6           What are the main reasons to buy Nvidia?   \n",
       "7  What is the current dividend yield for Alphabe...   \n",
       "\n",
       "                                      correct_answer  \\\n",
       "0                                           0.73 EPS   \n",
       "1                                    $2182.8 Billion   \n",
       "2                                               SELL   \n",
       "3                                               0.07   \n",
       "4                                     18,989 Million   \n",
       "5  missed consensus forecasts and strong total bo...   \n",
       "6  Datacenter, GPUs Demands, Self-driving, and ca...   \n",
       "7                                             0.0046   \n",
       "\n",
       "                                              answer  \n",
       "0  The provided passages do not contain any infor...  \n",
       "1  The market capitalization of Alphabet (Class A...  \n",
       "2  The provided passages contain no information a...  \n",
       "3              The current mortgage rate is near 7%.  \n",
       "4  The passages provided do not contain any infor...  \n",
       "5  The latest financial metrics for Accenture PLC...  \n",
       "6  The passage does not provide specific reasons ...  \n",
       "7  The passages provided do not mention a current...  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t.select(queries_t.Question, queries_t.correct_answer, queries_t.answer).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The application works, but, as expected, a few questions couldn't be answered due to the missing documents. As a final step, let's add the remaining documents to our document base, and run the queries again."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Incremental Updates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pixeltable's views and computed columns update automatically in response to new data. We can see this when we add the remaining documents to our `documents` table. Watch how the `chunks` view is updated to stay in sync with `documents`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inserting rows into `documents`: 3 rows [00:00, 1533.57 rows/s]\n",
      "Inserting rows into `chunks`: 68 rows [00:00, 754.41 rows/s]\n",
      "Inserted 71 rows with 0 errors.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "UpdateStatus(num_rows=71, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents_t.insert({'document': p} for p in document_urls[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>document</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_0f95e650ad40e78bbe8b427de7c9095a9b8fa24cf432708e6df3c69626e732be.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_a1ecd0cc068f969b047d54f4611ec5620f9b2fb92aeecb3a605106591e2e3980.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_66aa90e7cb16107a4d93664b7c4ae5f1619cf0b259780380e30f93f6127b7f02.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_2830bc5c2ecd6b8f73fe55b9f72f6ed615a7f0af2faa9f849188548dafe6d306.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_9797b893dae275e7ca2e55086569b010183c4f20a7a1af477abd4794844e0b13.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td><div class=\"pxt_document\" style=\"width:320px;\">\n",
       "            <a href=\"http://127.0.0.1:64872/Users/asiegel/.pixeltable/file_cache/ae7c9d97897f45f0b0ea4785da62513d_0_93e519a8498f03e7f19ad0cb4ba5f9904f15cf1a128af61200cdaf9e816296d5.pdf\">\n",
       "                \n",
       "                    <img style=\"object-fit: contain; border: 1px solid black;\" src=\"\" />\n",
       "                \n",
       "            </a>\n",
       "        </div></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                            document\n",
       "0  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "1  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "2  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "3  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "4  /Users/asiegel/.pixeltable/file_cache/ae7c9d97...\n",
       "5  /Users/asiegel/.pixeltable/file_cache/ae7c9d97..."
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents_t.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Note: although Pixeltable updates `documents` and `chunks`, it **does not** automatically update the `queries` table. This is by design: we don't want all rows in `queries` to get automatically re-executed every time a single new document is added to the document base. However, newly-added rows will be run over the new, incrementally-updated index.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To confirm that the `chunks` index got updated, we'll re-run the chunks retrieval query for the question\n",
    "\n",
    "```What is the expected EPS for Nvidia in Q1 2026?```\n",
    "\n",
    "Previously, our most similar chunk had a similarity score of ~0.81. Let's see what we get now:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>similarity</th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0.863</td>\n",
       "      <td>4\n",
       "7,192 A\n",
       "13,507 A\n",
       "18,120 A\n",
       "22,103 A\n",
       "60,922 A\n",
       "EPS Estimates(2)\n",
       " \n",
       "Q1\n",
       "Q2\n",
       "Q3\n",
       "Q4\n",
       "Annual*\n",
       "2026\n",
       "0.73 E\n",
       "0.73 E\n",
       "0.77 E\n",
       "0.81 E\n",
       "3.04 E\n",
       "2025\n",
       "0.61 A\n",
       "0.62 E\n",
       "0.67 E\n",
       "0.71 E\n",
       "2.62 E\n",
       "2024\n",
       "0.11 A\n",
       "0.27 A\n",
       "0.40 A\n",
       "0.52 A\n",
       "1.30 A\n",
       "*Quarterly figures may not add up to annual.\n",
       "1) The data in the charts and tables, except the estimates, is as of 06/11/2024.\n",
       "2) The report&#x27;s text, the analyst-provided estimates, and the price target are as of 06/12/2024.\n",
       "Zacks Report Date: June 12, 2024\n",
       "© 2024 Zacks Investment Research, All Rights Reserved\n",
       "10 S. Riverside Plaza Suite 1600 · Chicago, IL 60606\n",
       "Overview\n",
       "NVIDIA\n",
       "Corporation\n",
       "is the worldwide leader in visual\n",
       "computing\n",
       "technologies and the inventor of the graphic processing unit, or GPU.\n",
       "Over the years, the company&#x27;s focus has evolved from PC graphics to\n",
       "artificial\n",
       "intelligence\n",
       "(AI)\n",
       "based\n",
       "solutions\n",
       "that</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.855</td>\n",
       "      <td>and Microsoft&#x27;s Xbox One will also be going with AMD. NVIDIA also has limited scope for growth in\n",
       "the apps processor market as it is dominated by Apple, Samsung and Qualcomm. We believe that competitive pressure from two CPU\n",
       "vendors, Intel and AMD, who are planning to integrate graphics cores into their chips can negatively impact NVIDIA&#x27;s revenues in the long\n",
       "haul.\n",
       " \n",
       "A substantial portion of the company&#x27;s sales is derived from outside the United States. Sales revenues to customers outside  ...... stimates, Revenues Rise Y/Y\n",
       "NVIDIA reported first-quarter fiscal 2025 earnings of \\$6.12 per share, which beat the Zacks\n",
       "Consensus Estimate by 11.48% and increased 19% sequentially. Notably, NVDA posted\n",
       "earnings of \\$1.09 per share in the year-ago quarter.\n",
       "Revenues jumped 262% year over year to \\$26.04 billion and beat the Zacks Consensus\n",
       "Estimate by 7.02%. On a sequential basis, revenues increased 18%.\n",
       "NVIDIA is riding on a strong and innovative portfolio, with the growing adoption of its GPUs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.848</td>\n",
       "      <td>ations for Windows to deliver maximum performance on NVIDIA GeForce RTX AI PCs and workstations.\n",
       "For the Professional Visualization domain, it launched NVIDIA RTX 500 and 1000 professional Ada generation laptop GPUs for AI-enhanced\n",
       "workflows, NVIDIA RTX A400 and A1000 GPUs for desktop workstations and NVIDIA Omniverse Cloud APIs.\n",
       "Operating Details\n",
       "NVIDIA&#x27;s non-GAAP gross margin increased to 78.9% from 66.8% in the year-ago quarter and 76.7% from the previous quarter, mainly driven\n",
       "by higher  ...... ted benefits.\n",
       "However, as a percentage of total revenues, non-GAAP operating expenses declined to 9.6% from 24.3% in the year-ago quarter and 30.7% in\n",
       "the previous quarter.\n",
       "The non-GAAP operating income was \\$18.06 billion compared with \\$3.05 billion in the year-ago quarter. Sequentially, the figure jumped 22.4%.\n",
       "Balance Sheet and Cash Flow\n",
       "As of Apr 28, 2024, NVDA&#x27;s cash, cash equivalents and marketable securities were \\$31.44 billion, up from \\$25.98 billion as of Jan 28, 2024.\n",
       "As of Apr 28,</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.841</td>\n",
       "      <td>2024, the total long-term debt was \\$8.46 billion, unchanged sequentially.\n",
       "NVIDIA generated \\$15.4 billion in operating cash flow, up from the previous quarter&#x27;s \\$11.5 billion.\n",
       "The company ended the fiscal first quarter with a free cash flow of \\$14.94 billion.\n",
       "In the fiscal first quarter, it returned \\$7.8 billion to shareholders through dividend payouts and share repurchases.\n",
       "Guidance\n",
       "For the second quarter of fiscal 2025, NVIDIA anticipates revenues of \\$28 billion (+/-2%), higher than the Zac ......  IGX platform.\n",
       "On Jun 2, NVIDIA announced that the world&#x27;s leaders in robot development are adopting the NVIDIA Isaac robotics platform for the research,\n",
       "development and production of the next generation of AI-enabled autonomous machines and robots.\n",
       "On Jun 2, NVIDIA announced new NVIDIA RTX technology to power AI assistants and digital humans running on new GeForce RTX AI laptops.\n",
       "On Jun 2, NVIDIA announced the widespread adoption of the NVIDIA Spectrum-X Ethernet networking platform as well</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.84</td>\n",
       "      <td>9.78%.\n",
       "NVIDIA Drive Thor solution was adopted by BYD, XPENG, GAC&#x27;s AION Hyper, Nuro and others in the reported quarter. Lucid and IM Motors\n",
       "are using the NVIDIA DRIVE Orin platform for vehicle models targeting the European market.\n",
       "OEM and Other revenues moved up 1% year over year but declined 13% sequentially to \\$78 million. The figure missed the consensus mark by\n",
       "15.39%.\n",
       "Expanding Portfolio Aids Prospects\n",
       "In the fiscal first quarter, NVIDIA launched the Blackwell platform targeted for AI co ...... ucture.\n",
       "Moreover, the company launched NVIDIA AI Enterprise 5.0 with NVIDIA NIM inference microservices to speed enterprise app development.\n",
       "For the gaming domain, NVIDIA launched AI gaming technologies for NVIDIA ACE and Neural Graphics. Moreover, it unveiled AI performance\n",
       "FY Quarter Ending\n",
       "1/31/2024\n",
       "Earnings Reporting Date\n",
       "May 22, 2024\n",
       "Sales Surprise\n",
       "7.02%\n",
       "EPS Surprise\n",
       "11.48%\n",
       "Quarterly EPS\n",
       "0.61\n",
       "Annual EPS (TTM)\n",
       "1.80\n",
       "Zacks Equity Research\n",
       "www.zacks.com\n",
       "Page 5 of 10\n",
       "optimizations and integr</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "   similarity                                               text\n",
       "0    0.862984  4\\n7,192 A\\n13,507 A\\n18,120 A\\n22,103 A\\n60,9...\n",
       "1    0.854627   and Microsoft's Xbox One will also be going w...\n",
       "2    0.847631  ations for Windows to deliver maximum performa...\n",
       "3    0.841044  2024, the total long-term debt was $8.46 billi...\n",
       "4    0.840021  9.78%.\\nNVIDIA Drive Thor solution was adopted..."
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nvidia_eps_query.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our most similar chunk now has a score of ~0.86 and pulls in more relevant chunks from the newly-inserted documents.\n",
    "\n",
    "Let's force all the queries to run again by removing them from the `queries` table and re-inserting them as new rows. Note how the structure of the tables and computed columns is preserved, even after we delete the existing data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing cells: 100%|██████████████████████████████████████████| 56/56 [00:13<00:00,  4.15 cells/s]\n",
      "Inserting rows into `queries`: 8 rows [00:00, 511.80 rows/s]\n",
      "Computing cells: 100%|██████████████████████████████████████████| 56/56 [00:13<00:00,  4.14 cells/s]\n",
      "Inserted 8 rows with 0 errors.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "UpdateStatus(num_rows=8, num_computed_values=56, num_excs=0, updated_cols=[], cols_with_excs=[])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "questions = list(queries_t.select(queries_t.S__No_, queries_t.Question, queries_t.correct_answer).collect())\n",
    "queries_t.delete()\n",
    "queries_t.insert(questions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a final step, let's confirm that all the queries now have answers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Question</th>\n",
       "      <th>correct_answer</th>\n",
       "      <th>answer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>What is the expected EPS for Nvidia in Q1 2026?</td>\n",
       "      <td>0.73 EPS</td>\n",
       "      <td>The expected EPS (Earnings Per Share) for NVIDIA in Q1 2026 is 0.73.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the market capitalization of Alphabet?</td>\n",
       "      <td>\\$2182.8 Billion</td>\n",
       "      <td>The market capitalization of Alphabet is \\$2,182.8 billion.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the overall latest rating for Amazon.com from analysts?</td>\n",
       "      <td>SELL</td>\n",
       "      <td>The overall latest rating for Amazon.com (AMZN) from analysts is a &quot;SELL.&quot;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is roughly the current mortage rate?</td>\n",
       "      <td>0.07</td>\n",
       "      <td>The current mortgage rate is near 7%.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the operating cash flow of Amazon in Q1 2024?</td>\n",
       "      <td>18,989 Million</td>\n",
       "      <td>The operating cash flow of Amazon in Q1 2024 is reported as 12,232.0 million.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What are the latest financial metrics for Accenture PLC?</td>\n",
       "      <td>missed consensus forecasts and strong total bookings rising by 22% annually</td>\n",
       "      <td>The latest financial metrics for Accenture PLC based on the provided passages are as follows:\n",
       "\n",
       "1. **Free Cash Flow**:\n",
       "   - FY24 forecast: \\$8.7-\\$9.3 billion\n",
       "   - FY23: \\$9.0 billion\n",
       "   - FY22: \\$8.2 billion\n",
       "   - FY21: \\$8.4 billion\n",
       "   - FY20: \\$7.6 billion\n",
       "\n",
       "2. **Shareholder Returns**:\n",
       "   - Expected return for FY24: At least \\$7.7 billion\n",
       "   - FY23: \\$7.2 billion returned\n",
       "   - FY22: \\$6.1 billion returned\n",
       "   - FY21: \\$5.9 billion returned\n",
       "   - FY20: \\$5.0 billion returned\n",
       "   - FY19: \\$4.6 billion return ...... at the end of FY23: \\$9.05 billion\n",
       "\n",
       "7. **Cash Flow from Operations**:\n",
       "   - FY23: \\$9.52 billion\n",
       "   - FY22: \\$9.54 billion\n",
       "   - FY21: \\$8.98 billion\n",
       "   - FY20: \\$8.36 billion\n",
       "   - FY19: \\$6.63 billion\n",
       "\n",
       "8. **Credit Ratings**:\n",
       "   - Moody&#x27;s long-term credit rating: Aa3\n",
       "   - Standard &amp; Poor&#x27;s long-term rating: A+\n",
       "\n",
       "Overall, Accenture shows a strong financial position with solid free cash flow, strong dividends, and healthy revenue growth expectations despite some challenges in the operating environment.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What are the main reasons to buy Nvidia?</td>\n",
       "      <td>Datacenter, GPUs Demands, Self-driving, and cash-flow</td>\n",
       "      <td>The main reasons to buy NVIDIA, as highlighted in the passages, include:\n",
       "\n",
       "1. **Strong Cash Flow Generation**: NVIDIA has demonstrated robust operating and free cash flow, allowing it to invest in growth initiatives and return value to shareholders. In fiscal 2024, it generated significant amounts in cash flow (\\$28.09 billion operating cash flow and \\$11.22 billion free cash flow), which indicates a strong financial position.\n",
       "\n",
       "2. **Growth Opportunities in Various Markets**: The company is well ...... ics**: NVIDIA stocks have surged in value, and despite this growth, their forward P/E ratio is lower compared to the sub-industry and sector averages, indicating potential for further price appreciation.\n",
       "\n",
       "7. **Shareholder Returns**: NVIDIA is returning significant amounts to shareholders through dividends and share repurchases, indicating confidence in its financial health and commitment to shareholder value.\n",
       "\n",
       "These factors collectively position NVIDIA as a compelling investment opportunity.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>What is the current dividend yield for Alphabet Inc. (\\$GOOGL)?</td>\n",
       "      <td>0.0046</td>\n",
       "      <td>The current dividend yield for Alphabet Inc. (\\$GOOGL) is 0.46%.</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "                                            Question  \\\n",
       "0    What is the expected EPS for Nvidia in Q1 2026?   \n",
       "1     What is the market capitalization of Alphabet?   \n",
       "2  What is the overall latest rating for Amazon.c...   \n",
       "3          What is roughly the current mortage rate?   \n",
       "4  What is the operating cash flow of Amazon in Q...   \n",
       "5  What are the latest financial metrics for Acce...   \n",
       "6           What are the main reasons to buy Nvidia?   \n",
       "7  What is the current dividend yield for Alphabe...   \n",
       "\n",
       "                                      correct_answer  \\\n",
       "0                                           0.73 EPS   \n",
       "1                                    $2182.8 Billion   \n",
       "2                                               SELL   \n",
       "3                                               0.07   \n",
       "4                                     18,989 Million   \n",
       "5  missed consensus forecasts and strong total bo...   \n",
       "6  Datacenter, GPUs Demands, Self-driving, and ca...   \n",
       "7                                             0.0046   \n",
       "\n",
       "                                              answer  \n",
       "0  The expected EPS (Earnings Per Share) for NVID...  \n",
       "1  The market capitalization of Alphabet is $2,18...  \n",
       "2  The overall latest rating for Amazon.com (AMZN...  \n",
       "3              The current mortgage rate is near 7%.  \n",
       "4  The operating cash flow of Amazon in Q1 2024 i...  \n",
       "5  The latest financial metrics for Accenture PLC...  \n",
       "6  The main reasons to buy NVIDIA, as highlighted...  \n",
       "7  The current dividend yield for Alphabet Inc. (...  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "queries_t.select(queries_t.Question, queries_t.correct_answer, queries_t.answer).show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
